package com.lang.actions;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.lang.beans.User;
import com.lang.service.UserService;
import com.opensymphony.xwork2.Action;

public class UserAction implements Action {
	private User user;
	private List parcels;
	private UserService userservice;
	private String username;
	private String tel;
	private String email;
	private String password;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserservice() {
		return userservice;
	}

	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	public List getParcels() {
		return parcels;
	}

	public void setParcels(List parcels) {
		this.parcels = parcels;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getTel() {
		return tel;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @return
	 */
	public String getUserInfo() {
		String id = ServletActionContext.getRequest().getSession()
				.getAttribute("uid").toString();
		user = userservice.getUserInfo(id);

		if (user != null) {
			return "success";
		}
		return "error";
	}

	/**
	 * 获取用户包裹
	 * 
	 * @return
	 */
	public String getUserParcels() throws Exception {
		String id = ServletActionContext.getRequest().getSession()
				.getAttribute("uid").toString();
		System.out.println("id = " + id);
		parcels = userservice.getUserParcel(id);
		System.out.println("size = " + parcels.size());
		return "success";
	}
	/**
	 * 验证密码函数
	 * @return
	 * @throws Exception
	 */
	public String yzpwd()throws Exception{
		user=userservice.loginservice(getUsername(), getPassword());
        if (user!=null){
           ServletActionContext.getRequest().getSession().setAttribute("user", user);
           ServletActionContext.getRequest().getSession().setAttribute("uid", user.getId());
           return "success";
        }
        return "error";
	
	}
	public String yzpwd1()throws Exception{
		String id = ServletActionContext.getRequest().getSession()
				.getAttribute("uid").toString();
		user = userservice.getUserInfo(id);

		if (user != null) {
			return "success";
		}
		return "error";
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 修改用户基本信息
	 * 
	 * @return
	 */
	public String modifyUserInfo(){
		String id=ServletActionContext.getRequest().getSession().getAttribute("uid").toString();
		user=userservice.getUserInfo(id);
		user.setName(this.getUsername());
		user.setTel(this.getTel());
		user.setEmail(this.getEmail());
		user.setPwd(this.getPassword());
		userservice.modifyUserInfo(user);
		return "success";
	}
	
	/**
	 * 注册新用户
	 */
	public String registerUser(){
		user=new User();
		user.setName(this.getUsername());
		user.setTel(this.getTel());
		user.setEmail(this.getEmail());
		user.setPwd(this.getPassword());
		user.setLevel(new Integer(2));
		userservice.register(user);
		return "success";
	}
}
